package com.woosiyuan.atie.demo;

import android.content.Context;
import android.media.AudioManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import com.woosiyuan.wsyrobotdemo.R;

public class VolumePopupWindow extends PopupWindow implements SeekBar.OnSeekBarChangeListener {
    private static final int VOLUME_TYPE = AudioManager.STREAM_SYSTEM;
    private Context      context;
    private SeekBar      mVoiceSeekBar;
    private SeekBar      mCallseekBar;
    private AudioManager mAudioManager;
    private ImageView    minVolumeIv;

    public VolumePopupWindow(Context context, View contentView, int width, int height, boolean focusable) {
        super(contentView, width, height, focusable);
        this.context = context;
        mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        initView(contentView);
    }

    private void initView(View view) {
        mVoiceSeekBar = (SeekBar) view.findViewById(R.id.volume_popup_window_seekbar);
        mVoiceSeekBar.setMax(mAudioManager.getStreamMaxVolume(VOLUME_TYPE)-2);
        mVoiceSeekBar.setProgress(mAudioManager.getStreamVolume(VOLUME_TYPE));
        mVoiceSeekBar.setOnSeekBarChangeListener(this);
        view.findViewById(R.id.volume_popup_window_min_iv)
            .setOnClickListener(new View.OnClickListener() {
                @Override public void onClick(View v) {
                    //设置系统音量静音
                    mAudioManager.setStreamVolume(VOLUME_TYPE, 0, 0);
                    mVoiceSeekBar.setProgress(0);
                }
            });

        mCallseekBar = (SeekBar) view.findViewById(R.id.call_popup_window_seekbar);
        mCallseekBar.setMax(mAudioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL));
        mCallseekBar.setProgress(mAudioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL));
        mCallseekBar.setOnSeekBarChangeListener(this);
        view.findViewById(R.id.call_popup_window_min_iv)
                .setOnClickListener(new View.OnClickListener() {
                    @Override public void onClick(View v) {
                        //设置系统音量静音
                        mAudioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, 0, 0);
                        mCallseekBar.setProgress(0);
                    }
                });
        setContentView(view);
    }

    @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

    }

    @Override public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override public void onStopTrackingTouch(SeekBar seekBar) {
        if(seekBar.getId()==R.id.volume_popup_window_seekbar){
            //设置系统音量
            mAudioManager.setStreamVolume(VOLUME_TYPE, seekBar.getProgress(), 0);
        }else if(seekBar.getId()==R.id.call_popup_window_seekbar){
            mAudioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, seekBar.getProgress(), 0);
        }

    }
}
